package com.glsc.ngateway.oaflow.amqp;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.glsc.ngateway.common.base.domain.mysql.gateway.oaflow.xirtrade.TOaFlowXirTradeInfo;
import com.glsc.ngateway.common.base.enums.OaFlowSyncMessageDescriptionEnum;
import com.glsc.ngateway.common.base.enums.OaFlowXirTradeInfoStatusEnum;
import com.glsc.ngateway.common.base.repo.mysql.gateway.oaflow.TOaFlowXirTradeInfoRepository;
import com.glsc.ngateway.oaflow.exception.OaflowException;
import com.glsc.ngateway.oaflow.utils.MqUtil;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
public class XirReceiver {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String xirQueue = "gateway.queue.fwFlow.oaflow.xir";
    @Autowired
    TOaFlowXirTradeInfoRepository xirTradeInfoRepo;


    /**
     * JSON格式：workflowId:111,requestId:123456789,workflowName:测试流程标题,description:success
     */
    @RabbitListener(queues = {xirQueue})
    @RabbitHandler
    public void processMessage(Message messageObject, Channel channel) {
        String message = new String(messageObject.getBody());
        logger.info("gateway.queue.fwFlow.oaflow.xir，收到消息，message receiver:" + message);
        JSONObject msgJsonObject = JSONObject.parseObject(message);
        //这两个字段不能为空
        String workflowId = StrUtil.trim(msgJsonObject.getString("workflowId"));
        String requestId = StrUtil.trim(msgJsonObject.getString("requestId"));
        String workflowName = StrUtil.trim(msgJsonObject.getString("workflowName"));
        String description = StrUtil.trim(msgJsonObject.getString("description"));
        logger.info("开始进行 查询流程表单数据,msg:" + msgJsonObject);
        TOaFlowXirTradeInfo log = xirTradeInfoRepo.findByRequestId(requestId);
        Exception exception = null;
        boolean ackFlag = true;
        try {
            if (Objects.isNull(log)) {
                logger.info("未找到流程记录{}，不做处理", requestId);
            } else {
                OaFlowSyncMessageDescriptionEnum desc = OaFlowSyncMessageDescriptionEnum.valuesOf(description)
                        .orElseThrow(() -> new OaflowException("无法从oa同步，message.desc:" + description));
                logger.info(String.valueOf(log));
                //判断流程过程，并修改数据状态(OaFlowXirTradeInfoStatusEnum)
                switch (desc) {
                    case END:
                        log.setDealStatus(OaFlowXirTradeInfoStatusEnum.END.getCode());
                        break;
                    case PASS:
                        log.setDealStatus(OaFlowXirTradeInfoStatusEnum.BEGIN.getCode());
                        break;
                    case REJECT:
                        log.setDealStatus(OaFlowXirTradeInfoStatusEnum.CANCELLED.getCode());
                        break;
                    default:
                        logger.info("xir衡泰指令回调异常，未匹配到流程描述，requestId-{}，desc-{}", log.getRequestId(), desc);
                }
                xirTradeInfoRepo.save(log);
            }
        } catch (Exception e) {
            logger.info("gateway.queue.fwFlow.oaflow.xir，消息处理异常:" + e.getMessage());
            ackFlag = false;
            exception = e;
            logger.error("异常", e);
        }
        logger.info("gateway.queue.fwFlow.oaflow.xir，消息处理完毕，message:" + message);
        MqUtil.processAckFlag(messageObject, channel, ackFlag, "国联技术服务平台流程处理", exception);
    }


}
